const {Query}=require('../db/qurey')
const {getTime} = require("../utlis/date");

// 获取今日菜品
module.exports.getMenu= async () => {
    //获取日期
    let data=getTime()
    // console.log(data)
    return await Query(`SELECT * FROM menu WHERE menu_time='${data}';`)
}

// 获取所有历史菜品
module.exports.getHistory= async () => {
    return await Query(`SELECT * FROM menu;`)
}

//添加订单
module.exports.getAddOrdering= async (user_id,menu_id,menu_type) => {
    let data=getTime()
    let isOrdering= await Query(`SELECT * FROM ordering WHERE user_id='${user_id}' and menu_id='${menu_id}';`)
    // console.log(isOrdering)
    //辨别订单是否已经存在
    if (isOrdering[0]){
        let newCount=isOrdering[0].order_count+1
        // //订单已支付 再次订购
        // if (isOrdering[0].order_status=='true'){
        //     return await Query(`INSERT  INTO ordering (user_id,menu_id,order_time,menu_type)
        //     VALUES ("${user_id}","${menu_id}","${data}","${menu_type}");`)
        // }
        //订单已存在 订单数量+1
        return  await Query(`UPDATE ordering SET order_count='${newCount}' WHERE user_id='${user_id}' and menu_id='${menu_id}'`)
    }else {
        //订单不存在 添加新订单
        return await Query(`INSERT  INTO ordering (user_id,menu_id,order_time,menu_type) 
    VALUES ("${user_id}","${menu_id}","${data}","${menu_type}");`)
    }
}

//取消订单
module.exports.deleteCancelOrdering=async (user_id,menu_id,cancelValue)=>{
    console.log(user_id,menu_id,cancelValue)
    let isOrdering= await Query(`SELECT * FROM ordering WHERE user_id='${user_id}' and menu_id='${menu_id}';`)
    console.log(isOrdering)
    let newCount=isOrdering[0].order_count-1
    if (cancelValue=='all'){
    //取消此菜品订单
        return  await Query(`DELETE from ordering WHERE user_id='${user_id}' and menu_id='${menu_id}'`)
    }else {
    //此菜品订单数量-1
        return  await Query(`UPDATE ordering SET order_count='${newCount}' WHERE user_id='${user_id}' and menu_id='${menu_id}'`)
    }
}

//获取用户订单列表
module.exports.getOrdering= async (user_id) => {
    //今天的订单
    let data=getTime()
    return await Query(`SELECT * FROM ordering WHERE user_id='${user_id}' and order_time='${data}';`)
}

//获取用户订单菜品列表
module.exports.getUserOrdering= async (user_id) => {
    //今天的订单
    let data=getTime()
    return await Query(`SELECT * FROM menu WHERE menu_id in (SELECT menu_id FROM ordering WHERE user_id='${user_id}' and order_time='${data}');`)
}

//结算
//修改订单状态
module.exports.updateCloseOrdering=async (user_id,moneyValue,type)=>{
    //只修改今天的订单
    let data=getTime()
    return  (await Query(`UPDATE ordering SET order_status='true' WHERE user_id='${user_id}' and order_time='${data}' and menu_type='${type}'`)
    ,await this.updateUserMoney(user_id,moneyValue))
}

//根据id获取用户
module.exports.getUser=async (user_id)=>{
    return await Query(`SELECT * FROM user WHERE user_id='${user_id}';`)
}

//修改用户零钱积分
module.exports.updateUserMoney=async (user_id,moneyValue)=>{
    // let sqUser= await Query(`SELECT * FROM user WHERE user_id='${user_id}';`)
    let sqUser= await this.getUser(user_id)
    //零钱修改值
    let newMoney=sqUser[0].user_money+moneyValue
    return  await Query(`UPDATE user SET user_money='${newMoney}' WHERE user_id='${user_id}'`)
}
